/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.duty.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.duty.entity.ZlmtDutySheet;
import com.thinkgem.jeesite.modules.duty.entity.ZlmtMaintenanceRecord;
import com.thinkgem.jeesite.modules.duty.service.ZlmtDutySheetService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 值班记录管理Controller
 * @author johnKee
 * @version 2016-07-17
 */
@Controller
@RequestMapping(value = "${adminPath}/duty/zlmtDutySheet")
public class ZlmtDutySheetController extends BaseController {

	@Autowired
	private ZlmtDutySheetService zlmtDutySheetService;
	
	@ModelAttribute
	public ZlmtDutySheet get(@RequestParam(required=false) String id) {
		ZlmtDutySheet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zlmtDutySheetService.get(id);
		}
		if (entity == null){
			entity = new ZlmtDutySheet();
		}
		return entity;
	}
	

	@RequestMapping(value = {"list", ""})
	public String list(ZlmtDutySheet zlmtDutySheet, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZlmtDutySheet> page = zlmtDutySheetService.findPage(new Page<ZlmtDutySheet>(request, response), zlmtDutySheet);
		model.addAttribute("page", page);
		return "modules/duty/zlmtDutySheetList";
	}


	@RequestMapping(value = "form")
	public String form(ZlmtDutySheet zlmtDutySheet, Model model) {
		model.addAttribute("zlmtDutySheet", zlmtDutySheet);
		return "modules/duty/zlmtDutySheetForm";
	}


	@RequestMapping(value = "save")
	public String save(ZlmtDutySheet zlmtDutySheet, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zlmtDutySheet)){
			return form(zlmtDutySheet, model);
		}
		zlmtDutySheetService.save(zlmtDutySheet);
		addMessage(redirectAttributes, "保存值班记录成功");
		return "redirect:"+ Global.getAdminPath()+"/duty/zlmtDutySheet/?repage";
	}
	

	@RequestMapping(value = "delete")
	public String delete(ZlmtDutySheet zlmtDutySheet, RedirectAttributes redirectAttributes) {
		zlmtDutySheetService.delete(zlmtDutySheet);
		addMessage(redirectAttributes, "删除值班记录成功");
		return "redirect:"+ Global.getAdminPath()+"/duty/zlmtDutySheet/?repage";
	}


	/**
	 * 跳到交班页面  终结登录用户的值班记录
	 * @param
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toChangeDuty")
	public String toChangeDuty(Model model,RedirectAttributes redirectAttributes) {
		//获得当前登录用户信息
		User user = UserUtils.getUser();
		//存入  备用
		ZlmtDutySheet zlmtDutySheet = new ZlmtDutySheet();
		zlmtDutySheet.setUser(user);
		//查询未交接值班记录  如果没新添加
		zlmtDutySheet.setSysUserId(user.getId());
		zlmtDutySheet.setDutyStatus("0");// 0 未交接 1 已交接
 		List<ZlmtDutySheet> zlmtDutySheetList =  zlmtDutySheetService.findList(zlmtDutySheet);
		if (zlmtDutySheetList != null && zlmtDutySheetList.size() > 0) {
			 zlmtDutySheet = zlmtDutySheetList.get(0);
		}else{
			addMessage(redirectAttributes, "未查询到相关值班记录,重新添加");
		}
		model.addAttribute("zlmtDutySheet", zlmtDutySheet);
		return "modules/duty/zlmtChangeDutySheet";
	}

	/**
	 *交班后新增下一个值班记录
	 * @param zlmtDutySheet
	 * @param redirectAttributes
     * @return
     */
	@RequestMapping(value = "changeDuty")
	public String changeDuty(ZlmtDutySheet zlmtDutySheet, RedirectAttributes redirectAttributes,Model model) {
		if (!beanValidator(model, zlmtDutySheet)){
			return form(zlmtDutySheet, model);
		}
		zlmtDutySheet.setDutyStatus("1"); //0 未交接 1 已交接
		zlmtDutySheetService.save(zlmtDutySheet);
		 zlmtDutySheet = new ZlmtDutySheet();
		model.addAttribute("zlmtDutySheet", zlmtDutySheet);
		addMessage(redirectAttributes, "保存值班记录成功");
		return "modules/duty/zlmtAddNextDutySheet";
	}


}